package Main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailNotificator {

	private String recipients;

	public EmailNotificator() {

		recipients = "";
		
		FileReader input;
		try {
			input = new FileReader("recipients.txt");
			BufferedReader bufRead = new BufferedReader(input);
			recipients = bufRead.readLine();		
			bufRead.close();

		} catch (Exception e) {
			System.out.println("ERROR reading recipients.txt");
		}
		
		if (recipients.isEmpty())
		{
			recipients = "fabio@disneyresearch.com";
		}
		

//		recipients = "fabio@zuend.net";
//
//		recipients += ",schmist@gmail.com";
//		recipients += ",dezanets@gmail.com";
//		recipients += ",amit.bermano@gmail.com";
//		recipients += ",emari0p@gmail.com";
//		recipients += ",jschoch@ethz.ch";
//		recipients += ",dr.oliver.stadler@gmail.com";
//		recipients += ",dufourp83@gmail.com";
//		recipients += ",milz.vivien@gmail.com";

	}

	public void sendNotification(List<MovieDay> movieDays) {
		MovieDay today = movieDays.get(movieDays.size() - 1);
		MovieDay yersterday = movieDays.get(movieDays.size() - 2);

		List<Movie> newMovies = today.getNewMovies(yersterday);

		if (!newMovies.isEmpty()) {			
			sendEmail(newMovies);
		} else {
			System.out.println("No new movies today!");
		}

	}

	private void sendEmail(List<Movie> newMovies) {
		
		System.out.println("New movies are playing, sending emails to " + recipients);

		String body = "<h1>NEW MOVIES ARE PLAYING</h1><br/>";

		body += "<table cellspacing=\"5\">";
		for (Movie m : newMovies) {
			body += m.ToStringHTMLinTable();
		}
		body += "</table>";
		

		body += "<br/><br/><h1><a href=\"http://kino.fusun.ch\">http://kino.fusun.ch</a></h1>";

		body += "<small><br/>----------------------------------------<br/>Powered by <strong>fusun.ch</strong> and <strong>reshape.ch</strong> and dinimer.<br/>If you wish to unsubscribe from this mailing list then good luck!</small>";

		System.out.println("\n-------------------" + body + "-------------------\n");
		send("[kino.fusun.ch] New Movies!", body);

	}

	private void send(String subject, String body) {
		Properties props = new Properties();
		// props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.host", "mail.fusun.ch");
		// props.put("mail.smtp.socketFactory.port", "465");
		// props.put("mail.smtp.socketFactory.class",
		// "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		// props.put("mail.smtp.port", "465");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication("kino@fusun.ch",
								"kino123");
					}
				});

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("kino@fusun.ch"));
			message.setRecipients(Message.RecipientType.BCC,
					InternetAddress.parse(recipients));
			message.setSubject(subject);
			message.setContent(body, "text/html");

			Transport.send(message);

			System.out.println("Done");

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}

}
